﻿@{
    Lang lang = ViewBag.Lang;
    TGVR_Project.Models.FlightSearchModelView viewModel = (TGVR_Project.Models.FlightSearchModelView)ViewData["viewModel"];
}
@{
    PortalAccount.PortalUser LoginUser = ViewBag.LoginUser;
    
}
<style>
    .ways
    {
        margin-left: 10px;
        cursor: pointer;
        margin-bottom: 5px;
    }
</style>
<form method="post" action="/flightsearch/index" id="flightSearch" onsubmit="return checkDate();">
<div id="search-ticket">
    <div id="search-title">
        @{
            string strTemp = lang.FLIGHTSEARCH;
            string[] arrFlight = strTemp.Split(' ');
        }
        <span style="color: Red; font-size: 40px; font-weight: bolder">@arrFlight[0]</span>
        <span style="color: Black; font-size: 30px;">@strTemp.Substring(arrFlight[0].Length)</span>
    </div>
    <div id="form">
        <div class="ways">
            <input type="radio" name="ways" id="oneway" value="1" onclick="onOneWay()" @{if (ViewBag.ways == 1)
                                                                                         {<text>checked</text> }} /><span onclick="onOneWay()">@lang.ONEWAY</span>
            <input type="radio" name="ways" id="twoway" value="2" onclick="onTwoWay()" @{if (ViewBag.ways == 2)
                                                                                         {<text>checked</text> }}  /><span onclick="onTwoWay()">@lang.TWOWAY</span>
            
                <input id="FindCheapest" name="IsFindCheapest" type="checkbox" @(ViewBag.IsSearchCheapest != null ? "checked" : "")/>
                <span onclick="onFindCheapest()">@lang.FINDCHEAPEST</span>
            
        </div>
        <div class="combo-input left">
            <div class="input-title">
                @lang.FROMCITY</div>
            <input class="input" readonly id="fromcity" name="fromcity" required = "required" value="@(ViewData["fromcity"] != null ? ViewData["fromcity"] : "Ho Chi Minh (SGN)")"/>
        </div>
        <div class="combo-input left">
            <div class="input-title">
                @lang.DEPARTDATE</div>
            @(Html.Telerik().DatePicker()
                    .Name("DatePickerDepartDate")
                                            .HtmlAttributes(new { id = "DatePickerDepartDate_wrapper", @class = "input datetime", required = "required", type = "datetime" })
            .Min(viewModel.DatePickerDepartDate.MinDate.Value)
            .Max(viewModel.DatePickerDepartDate.MaxDate.Value)
            .ShowButton(true)
            .OpenOnFocus(true)
            .Value(viewModel.DatePickerDepartDate.SelectedDate.Value)
                    .Format("dd/MM/yyyy")
                            .ClientEvents(events => events.OnChange("onChangeDateStart"))
            .ClientEvents(m => m.OnClose("OnChoosenDepart"))
            )
        </div>
        <div class="combo-input left">
            <div class="input-title">
                @lang.ADULT (@lang.F12TN)</div>
            @(Html.Telerik().NumericTextBox().MinValue(1).MaxValue(9)
                                        .Name("Adult").MinValue(1).Value(@ViewData["adult"] == null ? 1 : Int32.Parse(@ViewData["adult"].ToString())).DecimalDigits(0)
                        .HtmlAttributes(new { required = "required" })
    )
        </div>
        <div class="combo-input left">
            <div class="input-title">
                @lang.BABY (@lang.F0T2)</div>
            @(Html.Telerik().NumericTextBox().MinValue(0).MaxValue(9)
                                                .Name("Baby").MinValue(0).Value(@ViewData["baby"] == null ? 0 : Int32.Parse(@ViewData["baby"].ToString())).DecimalDigits(0)
                        .HtmlAttributes(new { required = "required" })
                )
        </div>
        <div class="combo-input left clear">
            <div class="input-title">
                @lang.TOCITY</div>
            <input class="input" id="tocity" readonly name="tocity" required = "required"  value="@(ViewData["tocity"] != null ? ViewData["tocity"] : "Ha Noi (HAN)")"/>
        </div>
        <div class="combo-input left">
            <div class="input-title">
                @lang.RETURNDATE</div>
            @(Html.Telerik().DatePicker()
                    .Name("DatePickerReturnDate")
                                    .HtmlAttributes(new { id = "DatePickerReturnDate_wrapper", @class = "input datetime", required = "required", type = "datetime" })
            .Min(viewModel.DatePickerReturnDate.MinDate.Value)
                    .Max(viewModel.DatePickerReturnDate.MaxDate.Value)
                            .ShowButton(true)
                    .Value(viewModel.DatePickerReturnDate.SelectedDate)
                    .OpenOnFocus(true)
                    .Format("dd/MM/yyyy")
                                     .ClientEvents(events => events.OnChange("onChangeDateEnd").OnOpen("onTwoWay"))

    )
        </div>
        <div class="combo-input left">
            <div class="input-title">
                @lang.CHILD (@lang.F2T11)</div>
            @(Html.Telerik().NumericTextBox()
                    .Name("Child").MinValue(0).Value(@ViewData["child"] == null ? 0 : Int32.Parse(@ViewData["child"].ToString())).DecimalDigits(0).HtmlAttributes(new { required = "required" })
                )
        </div>
        <div class="combo-input left" style="margin-left: 20px;">
            <div class="input-title">
                &nbsp;</div>
            <div id="advanced">
                <button type="submit" style="float: right; width: 203px; height: 30px; margin: 0;
                    padding: 4px">@lang.SEARCH</button>
            </div>
        </div>
    </div>
</div>
</form>
@Html.Partial("Partials/ListCity")
<script type="text/javascript" src="/Scripts/2012.2.607/telerik.timepicker.min.js"></script>
<script>

    $("flightSearch").validate();

    var ways = @ViewBag.ways;

    function onChangeDateStart(e) {
        if (ways == 2) {
            var datepicker = $("#DatePickerDepartDate").val();
            $("#DatePickerReturnDate").val(datepicker);
        }
        var timePicker = $('#DatePickerReturnDate').data("tDatePicker");
        var value = $('#DatePickerDepartDate').data("tDatePicker").value();
        //$('#todate').data('tDatePicker').minDate
        timePicker.min(value);
    }
    function onChangeDateEnd(e) {
        var datepicker = $("#DatePickerReturnDate").val();
        if (datepicker != "") {
            ways = 2;
            $("#twoway").attr("checked", true);
        }
    }

    function onOneWay() {
        ways = 1;
        $("#DatePickerReturnDate").val("");
        $("#oneway").attr("checked", true);
    }

    function onFindCheapest(){
        $("#FindCheapest").attr("checked", !$("#FindCheapest").attr("checked"));
    }

    function onTwoWay(e) {
        ways = 2;
        var datepicker = $("#DatePickerDepartDate").val();
        
        $("#twoway").attr("checked", true);

        var  myDate= $("#DatePickerDepartDate").val();
        var dateParts = myDate.split("/");

        var dateFrom = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

        var_dump(dateFrom);

        var  myDate= $("#DatePickerReturnDate").val();
        var dateParts = myDate.split("/");

        var dateReturn = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
        var_dump(dateReturn);

        if(dateReturn==null)
        {
            $("#DatePickerReturnDate").val(datepicker);
        }

        if(dateFrom > dateReturn){
            $("#DatePickerReturnDate").val(datepicker);
        }
        var datePicker = $("#DatePickerReturnDate").data("tDatePicker");
                datePicker.open();
    }

    $("#DatePickerDepartDate").attr("readonly","true");
    $("#DatePickerReturnDate").attr("readonly","true");

    function checkDate(){
        var  myDate= $("#DatePickerDepartDate").val();
        var dateParts = myDate.split("/");

        var dateFrom = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

        var  myDate= $("#DatePickerReturnDate").val();
        var dateParts = myDate.split("/");

        var dateReturn = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);


        if(dateFrom > dateReturn){
            alert("@lang.DEPARTLESSRETURN");
            return false;
        }

        fromcity = $("#fromcity").val();
        tocity = $("#tocity").val();
        if(fromcity==tocity)
        {
            alert("@lang.DEPARTDIFFRETURN");
            return false;
        }
    }

    function OnChoosenDepart(e){
        //alert('OnChoosenDepart');
        var datePicker = $("#DatePickerDepartDate").data("tDatePicker");
            datePicker.close();
            if(ways==2){
                var datePicker = $("#DatePickerReturnDate").data("tDatePicker");
                datePicker.open();
            }
    }

   

</script>
